package com.tuyan.aixiang.component;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tuyan.aixiang.R;
import com.tuyan.aixiang.util.ConvertUtil;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;

public class MenuItem extends LinearLayout {

    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.iv_icon)
    ImageView ivIcon;

    @BindView(R.id.v_sperater)
    View vSep;

    public MenuItem(Context context) {
        this(context, null);
    }

    public MenuItem(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MenuItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View contentView = LayoutInflater.from(context).inflate(R.layout.menu_item, this);
        ButterKnife.bind(contentView, this);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MenuItem);
        int title = ta.getResourceId(R.styleable.MenuItem_tt, 0);
        int iconId = ta.getResourceId(R.styleable.MenuItem_ic, 0);
        boolean sep = ta.getBoolean(R.styleable.MenuItem_sep, false);
        ta.recycle();

        if(title > 0 && iconId > 0) {
            this.setUp(title, iconId, sep);
        }

    }

    public MenuItem setUp(int titleId, int iconId, boolean sep) {
        tvTitle.setText(getContext().getResources().getString(titleId));
        ivIcon.setImageResource(iconId);
        if(!sep) {
            vSep.setVisibility(INVISIBLE);
        }
        return this;
    }


    public MenuItem setUp(String title, int iconId, boolean sep) {
        tvTitle.setText(title);
        ivIcon.setImageResource(iconId);
        if(!sep) {
            vSep.setVisibility(INVISIBLE);
        }
        return this;
    }

    public MenuItem setTitle(String title) {
        tvTitle.setText(title);
        return this;
    }

    public MenuItem bottomSeperator() {
        vSep.setVisibility(VISIBLE);
        return this;
    }

    public MenuItem bottomGap() {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.setMargins(0,0,0, ConvertUtil.dp2px(getContext(), 9));
        setLayoutParams(lp);
        return this;
    }

}
